package com.ziyia.a2_10_all;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;

public class Menu2 extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu2);

        textView = findViewById(R.id.text);

        registerForContextMenu(textView);


    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, 10, 0, "红色");
        menu.add(0, 20, 0, "黑色");

        menu.setGroupCheckable(0, true, true);
        menu.setHeaderIcon(R.drawable.user);
        menu.setHeaderTitle("选择颜色");
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()) {
            case 10:
                textView.setTextColor(Color.RED);

                break;
            case 20:
                textView.setTextColor(Color.BLACK);
                break;
        }
        return true;
    }
}